<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Order;
use App\Models\Coupon;
use App\Models\BlindBoxOrder;
use App\Models\Recharge;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\VipPurchase;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Api\V1\Resources\OrderResource;
use App\Http\Controllers\Api\V1\Resources\BlindBoxOrderResource;
use App\Http\Controllers\Api\V1\Resources\GoldRecordResource;
use App\Http\Controllers\Api\V1\Resources\VipPurchaseResource;

class PaysController extends Controller
{
	public function payOrderByBalance(Order $order): OrderResource
	{
		$lock = Cache::lock('pay_order_by_balance', 1);

		try {
			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			
			$result = cashier('balance')->payOrder($order);

            return new OrderResource($result);

		} finally {
			$lock->release();
		}
	}

	public function payBoxOrderByBalance(BlindBoxOrder $order)
	{
		$lock = Cache::lock('pay_box_order_by_balance', 1);

		try {
			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			
			//$coupon = Coupon::find(request()->coupon_id);
			
			$result = cashier('balance')->payBoxOrder($order);
			
            return new BlindBoxOrderResource($result);

		} finally {
			$lock->release();
		}
	}

	public function payVipPurchaseByBalance(VipPurchase $purchase): VipPurchaseResource
	{
		$lock = Cache::lock('pay_vip_purchase_by_balance', 15);

		try {
			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $purchase);
		
			$result =  cashier('balance')->payVip($purchase);

            return new VipPurchaseResource($result);

		} finally {
			$lock->release();
		}
	}

    public function payOrderByAlipay(Order $order): JsonResponse
    {
	    $lock = Cache::lock('pay_order_by_alipay', 15);

	    try {

	    	if (!$lock->get()) fail('高峰拥挤，请再试');

		    $this->authorize('own', $order);
		    $result = cashier('alipay')->payOrder($order);

            return response()->json(['result' => $result, 'order_id' => $order->id]);

	    } finally {
	    	$lock->release();
	    }
    }

    public function payOrderByAliwap(Order $order): JsonResponse
    {
	    $this->authorize('own', $order);
	    $result = cashier('alipay')->payOrder($order);
    	return response()->json(['result' => $result, 'order_id' => $order->id]);
    }

	public function payGoldChargeByAlipay(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('alipay')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payGoldChargeByAliwap(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('alipay')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

    public function payOrderByAdaWxmini(Order $order): JsonResponse
    {
        $this->authorize('own', $order);
        $result = cashier('adawxmini')->payOrder($order);
        return response()->json(['result' => $result, 'order_id' => $order->id]);
    }

    public function payGoldChargeByAdaWxmini(GoldCharge $charge): JsonResponse
    {
        $this->authorize('own', $charge);
        $result = cashier('adawxmini')->payGoldCharge($charge);
        return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
    }

	public function payGoldChargeBySandwap(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('sandwap')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payGoldChargeBySandwx(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('sandwap')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}
}
